﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using ArmyCreator.Resources;
using System.Collections.Generic;

namespace ArmyCreator.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel()
        {
            Groups = new ObservableCollection<GroupViewModel>();
        }
        public ObservableCollection<GroupViewModel> Groups { get; set; }

        public bool IsDataLoaded
        {
            get;
            set;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        internal void CreateGroupPanoramaItems(Microsoft.Phone.Controls.Panorama panorama)
        {
            System.Windows.Controls.ControlTemplate template = ((Microsoft.Phone.Controls.PanoramaItem)panorama.Items[0]).Template;

            panorama.Items.Clear();

            foreach (var group in Groups)
            {
                panorama.Items.Add(new Microsoft.Phone.Controls.PanoramaItem() { DataContext = group, Template = template });
            }
        }

        internal void LoadData(DataModel.User user, int p)
        {
            if (user != null)
            {
                foreach (var group in user.Groups)
                {
                    GroupViewModel groupViewModel = new GroupViewModel();
                    groupViewModel.LoadData(group, p);
                    Groups.Add(groupViewModel);
                }

                this.IsDataLoaded = true;
            }
        }
    }
}